###取出单发中pattern不同的位点
setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM\\bayes_pvalue_beta0")
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")

i=1
fn1=paste0(group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file=file[file$normal_bayes_pvalue<0.05|file$tumor_bayes_pvalue<0.05,]
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$normal_bayes_pvalue<0.05,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$normal_bayes_pvalue<0.05,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$tumor_bayes_pvalue<0.05,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$tumor_bayes_pvalue<0.05,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:5],sep="_"),names(file)[6:8])
file$pattern2=""
file[file$pattern=="normal_up-tumor_nosig",]$pattern2=2
file[file$pattern=="normal_up-tumor_up",]$pattern2=5
file[file$pattern=="normal_down-tumor_nosig",]$pattern2=1
file[file$pattern=="normal_nosig-tumor_up",]$pattern2=7
file[file$pattern=="normal_down-tumor_up",]$pattern2=10
file[file$pattern=="normal_up-tumor_down",]$pattern2=11
file[file$pattern=="normal_down-tumor_down",]$pattern2=4
file[file$pattern=="normal_nosig-tumor_down",]$pattern2=8
file=file[,c(-8:-6)]
rt=file

for(i in 2:6){
fn1=paste0(group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file=file[file$normal_bayes_pvalue<0.05|file$tumor_bayes_pvalue<0.05,]
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$normal_bayes_pvalue<0.05,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$normal_bayes_pvalue<0.05,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$tumor_bayes_pvalue<0.05,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$tumor_bayes_pvalue<0.05,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:5],sep="_"),names(file)[6:8])
file$pattern2=""
file[file$pattern=="normal_up-tumor_nosig",]$pattern2=2
file[file$pattern=="normal_up-tumor_up",]$pattern2=5
file[file$pattern=="normal_down-tumor_nosig",]$pattern2=1
file[file$pattern=="normal_nosig-tumor_up",]$pattern2=7
file[file$pattern=="normal_down-tumor_up",]$pattern2=10
file[file$pattern=="normal_up-tumor_down",]$pattern2=11
file[file$pattern=="normal_down-tumor_down",]$pattern2=4
file[file$pattern=="normal_nosig-tumor_down",]$pattern2=8
file=file[,c(-8:-6)]
rt=merge(rt,file,by="unitID",all=T)
rt=tidyr::unite(rt,pattern2,pattern2.x,pattern2.y,sep="_",na.rm=TRUE)
}

findoppo=function(x){
out="other"
y=x[6]
a=unique(unlist(unlist(strsplit(y,"_"))))
if(length(a)>=2){
for(x in 1:(length(a)-1)){
if(abs(as.numeric(a[x])-as.numeric(a[x+1]))==1){
out="opposite_direction"
}
}
}
return(out)
}
rt$pattern2.statis=apply(rt,1,findoppo)